package com.bayair.doctor.utils.permisstion;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Build;

import com.bayair.doctor.R;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Setting;

import androidx.annotation.NonNull;

/**
 *  Description: 动态权限申请封装类
 *  版本： implementation 'com.yanzhenjie:permission:2.0.0-rc11'
 */
public class PermissionUtils {

    public static final String TAG = "PermissionUtils";

    public static void requestPermission(Activity activity, OnRequestPermissionListener listener,
                                         @NonNull String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (AndPermission.hasPermissions(activity, permissions)) {
                if (listener != null)
                    listener.onGranted();
                return;
            }
            AndPermission.with(activity)
                    .runtime()
                    .permission(permissions)
                    .onGranted(data -> {
                        listener.onGranted();
                    })
                    .onDenied(data -> {
                        listener.onDenied(false);

                        //申请失败
                        if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {

                            new AlertDialog.Builder(activity, R.style.Theme_AppCompat_Light_Dialog_Alert)
                                    .setTitle("权限申请")
                                    .setMessage("拒绝权限申请，将会发生不可预知的错误！")
                                    .setPositiveButton("确认", (dialog, witch) -> {
                                        requestAgain(activity, listener, permissions);
                                    })
                                    .setNegativeButton("取消", (dialog, witch) -> {
                                        listener.onDenied(true);
                                    })
                                    .setCancelable(false)
                                    .create()
                                    .show();
                        }


                    })
                    .start();
        } else {
            listener.onGranted();
        }
    }

    public interface OnRequestPermissionListener {
        void onGranted();

        void onDenied(boolean isAlways);

    }


    private static void requestAgain(Activity activity, OnRequestPermissionListener listener,
                                     String... permissions) {
        // 这些权限被用户总是拒绝。
        AndPermission.with(activity)
                .runtime()
                .setting()
                .onComeback(new Setting.Action() {
                    @Override
                    public void onAction() {
                        requestPermission(activity, listener, permissions);
                        // 用户从设置回来了。
                    }
                })
                .start();
    }
}

/**
 * private String permissions[]=new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION};
 * <p>
 * private void checkAppPermission() {
 * <p>
 * PermissionUtils.requestPermission(this, new OnRequestPermissionListener() {
 *
 * @Override public void onGranted() {
 * <p>
 * mBluetoothAdapter=BLEService.getInstance(getApplicationContext()).getBluetoothAdapter();
 * <p>
 * if (mBluetoothAdapter==null||!mBluetoothAdapter.isEnabled()){
 * Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 * startActivityForResult(intent,REQUEST_CODE_BLE);
 * }
 * <p>
 * ToastUtils.showShort("已经授权");
 * <p>
 * }
 * @Override public void onDenied(boolean isAlways) {
 * ToastUtils.showShort("未授权");
 * if (isAlways){
 * finish();
 * }
 * <p>
 * }
 * },permissions);
 * <p>
 * }
 */
